package com.shop.service.impl;
import com.shop.model.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String GOODS_PAGE_KEY="goods:page";
    private static final String GOODS_STATUS="goods:status";

    private static final String GOODS_CATEGORY="goods:category";
    private static final long CACHE_EXPIRE_TIME = 30;



    public void cacheGoodsPage(int page, List<Goods> goods){
        String key =GOODS_PAGE_KEY+page;
        redisTemplate.opsForValue().set(key,goods);
    }


    public List<Goods> getCacheGoodsPage(int page){
        String key=GOODS_PAGE_KEY+page;
        return (List<Goods>) redisTemplate.opsForValue().get(key);
    }

    public void cacheGoodsHot( String status,Integer page, List<Goods> goods){
        String key =GOODS_STATUS+status+page;
        redisTemplate.opsForValue().set(key,goods);
    }

    public List<Goods> getCacheGoodsHot(String status,Integer page){
        String key=GOODS_STATUS+status+page;
        return (List<Goods>) redisTemplate.opsForValue().get(key);
    }

    public void delCacheGoodsPage(String content,String status,String page){
        Set keys=redisTemplate.keys("goods:"+content+status+page);
        redisTemplate.delete(keys);
    }


    public void cacheGoodsCategory( Integer categoryId, Integer page,List<Goods> goods) {
        String key = GOODS_CATEGORY + categoryId + page;
        redisTemplate.opsForValue().set(key,goods);
    }

    public List<Goods> getCacheGoodsCategory(Integer categoryId, Integer page) {
        String key = GOODS_CATEGORY + categoryId + page;
        return (List<Goods>) redisTemplate.opsForValue().get(key);
    }

    public void delete(String content,String status,String page){
        Set keys= redisTemplate.keys("goods:" + content + status + page+"*");
        redisTemplate.delete(keys);
    }
}
